package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.Employee;
import cn.wolfcode.wms.domain.OrderBill;
import cn.wolfcode.wms.domain.OrderBillItem;
import cn.wolfcode.wms.mapper.OrderBillItemMapper;
import cn.wolfcode.wms.mapper.OrderBillMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.IOrderBillService;
import cn.wolfcode.wms.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

@Service
public class OrderBillServiceImpl implements IOrderBillService {

    @Autowired
    private OrderBillMapper orderBillMapper;
    @Autowired
    private OrderBillItemMapper orderBillItemMapper;

    public OrderBill getById(Long id) {
        return orderBillMapper.selectByPrimaryKey(id);
    }

    public void saveOrUpdate(OrderBill orderBill) {
        if (orderBill.getId() != null) {
            update(orderBill);
        } else {
            save(orderBill);
        }
    }

    /**
     * 具体的新增逻辑
     *
     * @param orderBill
     */
    private void save(OrderBill orderBill) {
        // 1. 获取当前登录用户,设置录入人相关信息
        Employee currentUser = UserContext.getCurrentUser();
        orderBill.setInputUser(currentUser);
        orderBill.setInputTime(new Date());
        // 2. 重置订单状态为未审核
        orderBill.setStatus(OrderBill.ORDER_BILL_STATUS_NORMAL);
        // 3. 计算总金额与总数量
        BigDecimal totalAmount = BigDecimal.ZERO;
        BigDecimal totalNumber = BigDecimal.ZERO;
        // 4. 遍历明细列表,用于计算总金额与总数量
        for (OrderBillItem item : orderBill.getItems()) {
            // 5. 计算明细的金额小计,设置到明细对象中
            BigDecimal amount = item.getNumber().multiply(item.getCostPrice()).setScale(2, RoundingMode.HALF_UP);
            item.setAmount(amount);

            totalAmount = totalAmount.add(amount);
            totalNumber = totalNumber.add(item.getNumber());
        }
        // 6. 为订单对象设置总金额与总数量
        orderBill.setTotalAmount(totalAmount);
        orderBill.setTotalNumber(totalNumber);
        // 7. 保存订单
        orderBillMapper.insert(orderBill);
        // 8. 为明细对象设置订单id,保存明细
        for (OrderBillItem item : orderBill.getItems()) {
            item.setBillId(orderBill.getId());
            orderBillItemMapper.insert(item);
        }
    }

    /**
     * 具体的更新逻辑
     *
     * @param orderBill
     */
    private void update(OrderBill orderBill) {
        // 1. 删除旧的明细
        orderBillItemMapper.deleteByBillId(orderBill.getId());
        // 2. 计算商品总金额与总数量
        BigDecimal totalAmount = BigDecimal.ZERO;
        BigDecimal totalNumber = BigDecimal.ZERO;
        // 3. 遍历订单明细,计算明细的金额小计
        for (OrderBillItem item : orderBill.getItems()) {
            // 4. 为明细设置订单id
            item.setBillId(orderBill.getId());
            BigDecimal amount = item.getNumber().multiply(item.getCostPrice()).setScale(2, BigDecimal.ROUND_HALF_UP);
            item.setAmount(amount);
            // 5. 更新明细
            orderBillItemMapper.insert(item);
            // 6. 累加总金额与总数量
            totalAmount = totalAmount.add(amount);
            totalNumber = totalNumber.add(item.getNumber());
        }
        // 7. 为订单设置总金额与总数量
        orderBill.setTotalAmount(totalAmount);
        orderBill.setTotalNumber(totalNumber);
        // 8. 更新订单
        orderBillMapper.updateByPrimaryKey(orderBill);
    }

    public void delete(Long id) {
        // 删除明细
        orderBillItemMapper.deleteByBillId(id);
        // 删除订单
        orderBillMapper.deleteByPrimaryKey(id);
    }

    public PageResult query(QueryObject qo) {
        int rows = orderBillMapper.queryForCount(qo);
        if (rows == 0) {
            return PageResult.empty(qo.getPageSize());
        }
        List<?> result = orderBillMapper.queryForList(qo);
        return new PageResult(rows, result, qo.getCurrentPage(), qo.getPageSize());
    }
}
